import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
import 'package:shopping_list/global.dart';

class GDatepicker extends StatefulWidget {
  GDatepicker({Key key, this.initDate, this.onChange}) : super(key: key);
  final initDate;
  final onChange;

  @override
  State<StatefulWidget> createState() => _GDatepickerState();
}

class _GDatepickerState extends State<GDatepicker> {
  String _dateTime = '';

  @override
  Widget build(BuildContext context) {
    return InkWell(
      child: Container(
        height: 48,
        padding: EdgeInsets.fromLTRB(10, 15, 15, 15),
        decoration: BoxDecoration(
          color: Global.fontSecondColor.withOpacity(0.08),
          borderRadius: BorderRadius.all(Radius.circular(7)),
        ),
        child: Text(_dateTime != '' ? _dateTime : widget.initDate),
      ),
      onTap: () {
        DatePicker.showDatePicker(
          context,
          pickerTheme: DateTimePickerTheme(
            confirm: Text('确定', style: TextStyle(color: Global.fontColor)),
          ),
          onMonthChangeStartWithFirstDate: true,
          minDateTime: DateTime.parse('2020-09-20'),
          maxDateTime: DateTime.parse('2022-12-31'),
          initialDateTime: DateTime.now(),
          dateFormat: 'yyyy-MMMM-dd',
          locale: DateTimePickerLocale.zh_cn,
          onConfirm: (dateTime, List<int> index) {
            setState(() {
              _dateTime = dateTime.toString().substring(0, 10);
              widget.onChange(_dateTime);
            });
          },
        );
      },
    );
  }
}
